package com.kgc.crowd.distributioncrowdwebui.controller;

import com.kgc.crowd.distributioncrowdcommon.api.MemberManagerRemoteService;
import com.kgc.crowd.distributioncrowdcommon.pojo.ResultEntity;
import com.kgc.crowd.distributioncrowdcommon.util.CrowdConstant;
import com.kgc.crowd.distributioncrowdcommon.vo.MemberSignSuccessVO;
import com.kgc.crowd.distributioncrowdcommon.vo.MemberVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class MemverController {
    @Resource
    MemberManagerRemoteService memberManagerRemoteService;

    @RequestMapping("/member/manager/to/mamber/center/page")
    public String toMemberCenter(){
        return "member";
    }

    @RequestMapping("/member/manager/login")
    public String login(MemberVO memberVO, Model model, HttpSession session){
        String loginacct=memberVO.getLoginacct();
        String userpawd=memberVO.getUserpswd();

        ResultEntity<MemberSignSuccessVO> loginResultEntity= memberManagerRemoteService.login(loginacct, userpawd);

        // String result=loginResultEntity.getResult();
        if (loginResultEntity.getResult().equals(ResultEntity.FAILED)){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,CrowdConstant.MESSAGE_LOGIN_FAILED);
            return "login";
        }
        MemberSignSuccessVO memberSignSuccessVO=loginResultEntity.getData();
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER,memberSignSuccessVO);

        return "redirect:/member/manager/to/mamber/center/page";
    }

    @RequestMapping("/member/manager/logout")
    public String logout(HttpSession session){
        //获取登录成功的memberSignSuccessVO对象
        MemberSignSuccessVO memberSignSuccessVO=(MemberSignSuccessVO)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        if(memberSignSuccessVO==null){
            return "redirect:/index";
        }
        String token=memberSignSuccessVO.getToken();
        ResultEntity<String> resultEntity=memberManagerRemoteService.logout(token);
        if (ResultEntity.FAILED.equals(resultEntity.getMessage())){
            throw new RuntimeException(resultEntity.getMessage());
        }
        session.invalidate();
        return "redirect:/index";
    }

    @RequestMapping("/member/manager/send/code")
    public ResultEntity<String> sendCode(@RequestParam("phoneNum")String phoneNum){
        ResultEntity<String> resultEntity=memberManagerRemoteService.sendCode(phoneNum);
        return resultEntity;
    }
    @RequestMapping("/member/manager/register")
    public String register(MemberVO memberVO,Model model){
        ResultEntity<String> register=memberManagerRemoteService.register(memberVO);
        if (register.getResult().equals(ResultEntity.FAILED)){
            model.addAttribute(CrowdConstant.ATTR_NAME_MESSAGE,register);
            return "reg";
        }
        return "login";
    }

    @RequestMapping("/member/manager/minecrowdfunding")
    public String minecrowdfunding(){
        return "minecrowdfunding";
    }


}
